<?php

class ControllerUserQuestion extends Controller
{

    private $error = array();


    public function index()
    {

        $this->document->setTitle('问卷调查列表');


        $this->load->model('user/question');

        $this->getQuestions();
    }


    public function edit(){

        $this->document->setTitle('编辑问卷调查');

        $this->load->model('user/question');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
            $this->model_user_question->editQuestion($this->request->get['question_id'],$this->request->post);
            $this->session->data['success'] = "编辑成功";

            $this->response->redirect($this->url->link('user/question', 'token=' . $this->session->data['token'] , 'SSL'));
        }

        $this->getQuestionForm();
    }






    public function add(){
        $this->document->setTitle('添加调研问卷');

        $this->load->model('user/question');

        if (($this->request->server['REQUEST_METHOD'] == 'POST') ) {

            if( $this->validateForm()){

                $this->model_user_question->addQuestion($this->request->post);

                $this->session->data['success'] = "新增成功";

                $this->response->redirect($this->url->link('user/question', 'token=' . $this->session->data['token'] , 'SSL'));
            }
        }

        $this->getQuestionForm();
    }



    public function getQuestionForm()
    {
        $this->load->model('user/question');

        $data = array();


        $data['text_form'] = ! isset($this->request->get['question_id']) ? '添加调研问卷' : '编辑研问卷';


        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        }

        if (isset($this->error['name'])) {
            $data['error_name'] = $this->error['name'];
        }
        if (isset($this->error['url'])) {
            $data['error_url'] = $this->error['url'];
        }



        $data['cancel'] = $this->url->link('user/question', 'token=' . $this->session->data['token'] , 'SSL');

        if (isset($this->request->get['question_id']) ) {
            $question_info = $this->model_user_question->getQuestion($this->request->get['question_id']);
        }


        if (isset($this->request->post['name'])) {
            $data['name'] = $this->request->post['name'];
        } elseif (!empty($question_info)) {
            $data['name'] = $question_info['name'];
        } else {
            $data['name'] = '';
        }

        if (isset($this->request->post['url'])) {
            $data['url'] = $this->request->post['url'];
        } elseif (!empty($question_info)) {
            $data['url'] = $question_info['url'];
        } else {
            $data['url'] = '';
        }





        if (!isset($this->request->get['question_id'])) {
            $data['action'] = $this->url->link('user/question/add', 'token=' . $this->session->data['token'] , 'SSL');
        } else {
            $data['action'] = $this->url->link('user/question/edit', 'token=' . $this->session->data['token'] . '&question_id=' . $this->request->get['question_id'] , 'SSL');
        }

        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');

        $this->response->setOutput($this->load->view('user/question_form.tpl', $data));
    }

    protected function getQuestions()
    {


        $data['questions'] = array();

        $results = $this->model_user_question->getQuestions();

        foreach ($results as $result) {
            $data['questions'][] = array(
                'question_id' => $result['question_id'],
                'name' => $result['name'],
                'url' => $result['url'],
                'date_added' => $result['date_added'],
                'edit' => $this->url->link('user/question/edit', 'token=' . $this->session->data['token'] . '&question_id=' . $result['question_id'] , 'SSL')
            );
        }

        $data['text_list'] = $this->language->get('text_list');
        $data['text_no_results'] = $this->language->get('text_no_results');
        $data['text_confirm'] = $this->language->get('text_confirm');
        $data['text_missing'] = $this->language->get('text_missing');
        $data['add'] = $this->url->link('user/question/add', 'token=' . $this->session->data['token'] , 'SSL');
        $data['delete'] = $this->url->link('user/question/delete', 'token=' . $this->session->data['token'] , 'SSL');

        $data['button_add'] = $this->language->get('button_add');
        $data['button_edit'] = $this->language->get('button_edit');
        $data['button_delete'] = $this->language->get('button_delete');
        $data['token'] = $this->session->data['token'];

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $data['success'] = '';
        }

        if (isset($this->request->post['selected'])) {
            $data['selected'] = (array) $this->request->post['selected'];
        } else {
            $data['selected'] = array();
        }


        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $this->response->setOutput($this->load->view('user/question_list.tpl', $data));
    }

    public function delete() {

        $this->load->model('user/question');

        if (isset($this->request->post['selected']) ) {
            foreach ($this->request->post['selected'] as $question_id) {
                $this->model_user_question->deleteQuestion($question_id);
            }

            $this->session->data['success'] = '删除成功';

            $this->response->redirect($this->url->link('user/question', 'token=' . $this->session->data['token'] , 'SSL'));
        }

        $this->getList();
    }

    protected function validateForm() {


        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }


        if (utf8_strlen($this->request->post['name']) < 1 ) {
            $this->error['name'] = "请输入名字";
        }
        if (utf8_strlen($this->request->post['url']) < 1 ) {
            $this->error['url'] = "请输入链接";
        }

        return !$this->error;
    }
}